package com.example.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@Component
public class GetResourcesFile {
    @Autowired
    ResourceLoader resourceLoader;

    public String getResourcesFile(){
        Resource resource = resourceLoader.getResource("classpath:img/1.txt");
        //获1.txt的取相对路径
        String path = null;
        try {
            path = resource.getFile().getPath();
            //读取1.txt中的内容
            InputStream is = resource.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String data = null;

            br.close();
            isr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return path;
    }

    public Resource getResources(String s){
        Resource resource = resourceLoader.getResource("classpath:img/" + s);
        return resource;
    }
}
